package com.xm.xmapp.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xm.xmapp.response.bean.CustomImageItem;

public class ShowImageAdapder extends PagerAdapter {
	private List<CustomImageItem> dataList;
	private ArrayList<String> dataList2;
	private ArrayList<ImageView> mViews = new ArrayList<ImageView>();

	public ShowImageAdapder(List<CustomImageItem> dataList, Activity activity) {
		this.dataList = dataList;
		int size = dataList.size();
		for (int i = 0; i != size; i++) {
			ImageView iv = new ImageView(activity);
			ImageLoader.getInstance().displayImage("file://" + dataList.get(i).sourcePath, iv);
			iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			mViews.add(iv);
		}
	}

	public ShowImageAdapder(ArrayList<String> dataList2, Activity activity) {
		this.dataList2 = dataList2;
		int size = dataList2.size();
		for (int i = 0; i != size; i++) {
			ImageView iv = new ImageView(activity);
			ImageLoader.getInstance().displayImage(dataList2.get(i), iv);
			iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			mViews.add(iv);
		}
	}

	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	public Object instantiateItem(View arg0, int arg1) {
		ImageView iv = mViews.get(arg1);
		((ViewPager) arg0).addView(iv);
		return iv;
	}

	public void destroyItem(View arg0, int arg1, Object arg2) {
		if (mViews.size() >= arg1 + 1) {
			((ViewPager) arg0).removeView(mViews.get(arg1));
		}
	}

	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public int getCount() {
		if (dataList != null) {
			return dataList.size();
		} else {
			return dataList2.size();
		}
	}

	public void removeView(int position) {
		if (position + 1 <= mViews.size()) {
			mViews.remove(position);
		}
	}

}
